package com.ciwong.epaper.modules.msg.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.msg.bean.MessageInfo;
import com.ciwong.epaper.modules.msg.bean.MsgTypeConfig;
import com.ciwong.epaper.modules.msg.util.MsgDateFormat;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by res-yinxue on 2016/6/16.
 */
public class WorkMessageAdapter extends BaseAdapter {
    /**
     * 上下文对象
     */
    private Context mContext;
    /**
     * 消息列表
     */
    private List<MessageInfo> infos;
    /**
     * 消息类型
     */
    private int type;

    /**
     * 构造器
     *
     * @param context
     * @param infos
     */

    public WorkMessageAdapter(Context context, List<MessageInfo> infos, int msgGroupType) {
        this.mContext = context;
        this.infos = infos;
        this.type = msgGroupType;
    }


    @Override
    public int getCount() {
        return infos == null ? 0 : infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos == null || infos.isEmpty() ? null : infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
      final   ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_work_msg, null);
            holder.timeTv = (TextView) convertView.findViewById(R.id.work_msg_time_tv);
            holder.titleTv = (TextView) convertView.findViewById(R.id.work_msg_title_tv);
            holder.contentTv = (TextView) convertView.findViewById(R.id.work_msg_content_tv);
            holder.time2Tv = (TextView) convertView.findViewById(R.id.work_msg_time2_tv);
            holder.detailTv = (TextView) convertView.findViewById(R.id.work_msg_detail_tv);
            holder.picIv = (ImageView) convertView.findViewById(R.id.work_msg_iv);
            holder.linearLayoutView = convertView.findViewById(R.id.work_item_view);
            holder.nameTv = (TextView) convertView.findViewById(R.id.work_msg_name_tv);

            holder.picLl = convertView.findViewById(R.id.work_msg_ll);
            holder.lineView = convertView.findViewById(R.id.work_msg_v);
            holder.relativeLayout = convertView.findViewById(R.id.work_item_rlview);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (type == MsgTypeConfig.MSG_SERVICE||type == MsgTypeConfig.MSG_SERVICE_NEW) {//MsgTypeConfig.MSG_SERVICE==30或者31时，为运营商消息

            //发送时间
            holder.timeTv.setText(MsgDateFormat.formatDate(infos.get(position).getPublishDate() * 1000));
            //具体内容
            String detailText = infos.get(position).getContent().getContent();
            String url = infos.get(position).getContent().getImgUrl();
            String href = infos.get(position).getContent().getHref();


            if (TextUtils.isEmpty(url)) {
                holder.picIv.setEnabled(false);
                holder.picLl.setVisibility(View.GONE);
                holder.picIv.setVisibility(View.GONE);
            }else {
                holder.picIv.setEnabled(true);
                //暂时设置为默认图片
                try {
                    Glide.with(mContext).load(url).asBitmap().fitCenter().placeholder(R.mipmap.msg_bottom_bg).error(R.mipmap.msg_bottom_bg).into(holder.picIv);
                }catch (Exception e){
                }
                holder.picLl.setVisibility(View.VISIBLE);
                holder.picIv.setVisibility(View.VISIBLE);
            }


            if (detailText != null && !detailText.isEmpty()) {
                holder.detailTv.setText(detailText);
                holder.detailTv.setVisibility(View.VISIBLE);

            } else {
                holder.detailTv.setVisibility(View.GONE);
            }


            if (TextUtils.isEmpty(href)) {
                holder.lineView.setVisibility(View.GONE);
                holder.relativeLayout.setVisibility(View.GONE);
            }else {
                holder.lineView.setVisibility(View.VISIBLE);
                holder.relativeLayout.setVisibility(View.VISIBLE);
            }

            //隐藏其他控件
            holder.titleTv.setVisibility(View.GONE);
            holder.contentTv.setVisibility(View.GONE);
            holder.time2Tv.setVisibility(View.GONE);
            holder.nameTv.setVisibility(View.GONE);

        } else {

            //发送者名字
            holder.titleTv.setText(infos.get(position).getTitle());
            //相关介绍
            holder.contentTv.setText(infos.get(position).getMsgIntr());
            //发送时间
            holder.timeTv.setText(MsgDateFormat.formatDate(infos.get(position).getPublishDate() * 1000));
            //发送者名称
            String nameText = infos.get(position).getContent().getWorkName();

            //起始时间
            String pubDateText = MsgDateFormat.formatDate(infos.get(position).getContent().getPublishDate() * 1000);
            //结束时间
            String effDateText = MsgDateFormat.formatDate(infos.get(position).getContent().getEffectiveDate() * 1000);

            //如果期限时间或发送者为空的话，不显示
            if (pubDateText != null && !pubDateText.isEmpty() && effDateText != null && !effDateText.isEmpty()
                    && infos.get(position).getMsgType() != MsgTypeConfig.MSG_WORK_EVALUATE
                    &&infos.get(position).getMsgType() != MsgTypeConfig.MSG_WORK_CORRECT) {
                String timeString = "作业时间：" + pubDateText + "~" + effDateText;

                holder.time2Tv.setText(timeString);
                holder.time2Tv.setVisibility(View.VISIBLE);


            } else {
                holder.time2Tv.setVisibility(View.GONE);
            }

            if (nameText != null && !nameText.isEmpty()) {
                String detailString = "作业名称：" + nameText;
                holder.nameTv.setText(detailString);
                holder.nameTv.setVisibility(View.VISIBLE);
            } else {
                holder.nameTv.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    private static class ViewHolder {
        TextView timeTv, titleTv, contentTv, time2Tv, detailTv, nameTv;
        ImageView picIv;
        View linearLayoutView, relativeLayout, lineView,picLl;
    }

    /**
     * 添加数据
     * <p/>
     * <p/>
     * 我的消息列表集合
     */

    public void addAll(List<MessageInfo> infos) {
        if (this.infos == null) {
            this.infos = new ArrayList<MessageInfo>();
        }
        if (infos != null) {
            for (MessageInfo info : infos) {

                this.infos.add(info);

            }
            notifyDataSetChanged();
        }
    }

    /**
     * 清空数据
     */
    public void clear() {
        if (infos != null) {
            infos.clear();
            notifyDataSetChanged();
        }
    }

    public void isServiceType() {

    }
}
